/*-------------------------------------------------------------------------
 * 版权所有：	黑客马拉松
 * 作者：		罗毅
 * 联系方式：	whluoyi@gmail.com
 * 创建时间：	2016/10/29 11:23:16
 * 版本号：		v1.0
 * 用途描述：	控制器
 *  -------------------------------------------------------------------------*/

namespace SuperMarieApi.App.Controllers.V1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    using FaceppSDK;

    using SuperMarieApi.App.Controllers.Biz;
    using SuperMarieApi.App.Controllers.Bll;
    using SuperMarieApi.App.Controllers.Entity;
    using SuperMarieApi.App.Controllers.Framework.Common;
    using SuperMarieApi.App.Controllers.Tool.Extensions;

    public class MyFaceToolController : ApiController
    {
        public static List<MyImgEntity> Model = new List<MyImgEntity>();

        // GET api/MyImg
        public IEnumerable<MyImgEntity> Get()
        {
            return Model;
        }

        // GET api/MyImg/5
        public MyImgEntity Get(int id)
        {
            var entity = Model.FirstOrDefault(i => i.MyImg_ID == id);
            return entity;
        }



        // GET api/v1/MyFaceTool?MyImg_ID=1&Readtype=2
        public HttpResponseMessage Get(int MyImg_ID, int Readtype)
        {
            //当前图片编号
            //调用face++ 存储检测结果
            //读取音乐列表
            DetectResult res = AppRootBiz.ReadImage(MyImg_ID);
            string allres = StringHelper.GetJSON<DetectResult>(res);
            MySessionEntity entity = new MySessionEntity();

            entity.MyImg_ID = MyImg_ID;
            entity.session_id = res.session_id;
            entity.url = res.url;
            entity.img_id = res.img_id;
            entity.face_id = ((FaceppSDK.Face[])(res.face))[0].face_id;
            //StringHelper.GetJSON<IList<Face>>(res.face);
            entity.DetectResult = allres;
            entity.AddTime = DateTime.Now;
            MySessionBiz.InsertMySessionObj(entity);

            MyFaceEntity faceEntity = new MyFaceEntity();
            faceEntity.face_id = entity.face_id;
            MyFaceBiz.InsertMyFaceObj(faceEntity);

            string json = "";
            List<MyMusicLabEntity> musiclist = AppRootBiz.ReadMusic(faceEntity.face_id);
            if (musiclist.Count > 0)
            {
                json = StringHelper.GetJSON<List<MyMusicLabEntity>>(musiclist);
            }
            else
            {
                json = "{_face_id:" + faceEntity.face_id + "}";
            }

            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content =
                    new StringContent(
                    json,
                    System.Text.Encoding.UTF8,
                    "application/json")
            };
        }


        // GET api/v1/MyImg?id=1&compare=1&other=1
        public HttpResponseMessage Get(int id, int compare, int other)
        {
            DetectResult res = AppRootBiz.ReadImage(id);
            string allres = StringHelper.GetJSON<DetectResult>(res);
            MySessionEntity entity = new MySessionEntity();

            entity.MyImg_ID = id;
            entity.session_id = res.session_id;
            entity.url = res.url;
            entity.img_id = res.img_id;
            entity.face_id = StringHelper.GetJSON<IList<Face>>(res.face);
            entity.DetectResult = allres;
            entity.AddTime = DateTime.Now;
            MySessionBiz.InsertMySessionObj(entity);

            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content =
                    new StringContent(
                    "<em style='color:red'>成功响应(id,level)</em>",
                    System.Text.Encoding.UTF8,
                    "text/html")
            };
        }


        // POST api/v1/MyFaceTool
        public HttpResponseMessage Post([FromBody] MyImgEntity value)
        {
            return Get(value.MyImg_ID.ToInt32(0), 2);
        }

        // PUT api/values?id=5
        public HttpResponseMessage Put(int id, [FromBody] MyImgEntity value)
        {
            var entity = Model.FirstOrDefault(i => i.MyImg_ID == id);
            entity.MyImg_ID = value.MyImg_ID;
            entity.FileUrl = value.FileUrl;
            entity.FilePath = value.FilePath;
            entity.Filebase64 = value.Filebase64;
            entity.AddTime = value.AddTime;
            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content =
                    new StringContent(
                    "{\"error_code\": 0,\"}", System.Text.Encoding.UTF8,
                    "application/json")
            };
        }

        // DELETE api/values/5
        public HttpResponseMessage Delete(int id)
        {
            Model.Remove(Model.FirstOrDefault(i => i.MyImg_ID == id));
            return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("删除数据成功") };
        }
    }
}